from sanic import Blueprint
from controllers.stock import StockController
from decorators import response_json
from utils.parser import parse_args

bp = Blueprint("stock", url_prefix="/stock")


@bp.route("", methods=["GET"])
@response_json
async def stocks(request):
    """ get stocks list """
    return StockController.get_stocks()

@bp.route("search", methods=["GET"])
@response_json
async def search(request):
    args_rules = [
        dict(arg="s", required=True, type="str")
    ]
    args = parse_args(request, "args", args_rules)
    return StockController.search(args["s"])

@bp.route("/<ts_code:str>/daily", methods=["GET"])
@response_json
async def daily(request, ts_code):
    """ get daily """
    return StockController(ts_code).get_daily()

@bp.route("/<ts_code:str>/indicator/<indicator:str>", methods=["GET"])
@response_json
async def indicator(request, ts_code, indicator):
    """ get indicator """
    return StockController(ts_code).get_indicator(indicator)

@bp.route("/run_indicator_strategy", methods=["GET"])
@response_json
async def run_indicator_strategy(request):
    args_rules = [
        dict(arg="cal_date", dest="cal_date_int", required=False, type="int")
    ]
    args = parse_args(request, "args", args_rules)
    return StockController.run_all(args.get("cal_date_int"))

@bp.route("/send_trigger_msg", methods=["GET"])
@response_json
async def send_trigger_msg(request):
    args_rules = [
        dict(arg="cal_date", dest="cal_date_int", required=False, type="int")
    ]
    args = parse_args(request, "args", args_rules)
    return StockController.send_trigger_msg(args.get("cal_date_int"))

@bp.route("/<ts_code:str>/strategy", methods=["GET"])
@response_json
async def indicator(request, ts_code):
    """ get stock strategy """
    args_rules = [
        dict(arg="orient", required=True, type="int"),
        dict(arg="cal_date", dest="cal_date_int", required=False, type="int")
    ]
    args = parse_args(request, "args", args_rules)
    return StockController(ts_code).get_stock_strategy(**args)

